<template>
  <div class="app-container">
    <!-- 查询区域 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" @keyup.enter.native="handleQuery">
      <el-form-item label="任务名称" prop="jobNameKey">
        <el-input v-model="queryParams.jobNameKey" placeholder="请输入任务名称" clearable  />
      </el-form-item>
      <el-form-item label="任务分组" prop="jobGroup">
        <lam-dict-select v-model="queryParams.jobGroup" dictCode="job_group" clearable placeholder="" style="width: 100%;" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <lam-dict-select v-model="queryParams.status" dictCode="job_status" placeholder="请选择状态" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="tool-bar-btn">
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPerms="['sched:job:add']">新增</el-button>
        <el-button type="primary" icon="el-icon-plus" :disabled="single" @click="handleCopy" v-hasPerms="['sched:job:add']">复制</el-button>
        <el-button type="success" icon="el-icon-edit"  :disabled="single" @click="handleUpdate" v-hasPerms="['sched:job:edit']">修改</el-button>
        <el-button type="danger" icon="el-icon-delete"  :disabled="multiple" @click="handleDelete" v-hasPerms="['sched:job:remove']">删除</el-button>
        <el-button type="warning" icon="el-icon-download"  @click="handleExport" v-hasPerms="['sched:job:export']">导出</el-button>
        <el-button type="danger" icon="el-icon-caret-right" :loading="exeBtnLoading" :disabled="single" @click="handleExe"
            v-hasPerms="['sched:job:execute']">执行</el-button>
        <el-button type="info" icon="el-icon-tickets" @click="handleLog" v-hasPerms="['sched:jobLog:list']">日志</el-button>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>

    <selected-info :count="selectedRows.length" :onClearSelected="onClearSelected" />
    <el-table :ref="dataTableRef" v-loading="loading" :data="dataList" stripe border @selection-change="onSelectChange"
              :default-sort = "defaultSort" @sort-change="onSortChange" :row-key="rowKey" >
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
      <el-table-column label="编号" align="center" prop="id" sortable="custom" fixed="left"/>
      <el-table-column label="任务名称" align="center" prop="jobName" width="180" fixed="left"/>
      <el-table-column label="任务分组" align="center" prop="jobGroupDictText" width="120"/>
      <el-table-column label="任务状态" align="center" prop="statusDictText" width="120">
        <template slot-scope="{row}">
          <el-switch v-model="row.status" :active-value="1" :inactive-value="0" @change="handleStatusChange(row)" />
        </template>
      </el-table-column>
      <el-table-column label="调度状态" align="center" prop="scheduleStatusDictText" width="180" :show-overflow-tooltip="true">
        <template slot-scope="{row}">
          <JobScheduleStatus :value="row.scheduleStatus" :text="row.scheduleStatusDictText" />
        </template>
      </el-table-column>
      <el-table-column label="con表达式" align="center" prop="cronExpression" width="150"/>
      <el-table-column label="表达式描述" align="center" prop="cronExpDesc" width="180"/>
      <el-table-column label="执行类" align="center" prop="exeClass" width="300" :show-overflow-tooltip="true"/>
      <el-table-column label="执行方法" align="center" prop="exeMethod" width="120"/>
      <el-table-column label="执行参数" align="center" prop="exeParam" width="200" :show-overflow-tooltip="true"/>
      <el-table-column label="并发类型" align="center" prop="concurrentTypeDictText" width="100"/>
      <el-table-column label="记录日志" align="center" prop="collectLogDictText" width="100"/>
      <el-table-column label="修改人" align="center" prop="updateBy" width="100"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180"/>
      <el-table-column label="备注信息" align="center" prop="remark" width="200" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="360">
        <template slot-scope="{row}">
          <el-button type="text" @click="showNodeDetail(row)" v-hasPerms="['sched:job:nodeDetail']">
            <svg-icon icon-class="sisternode" />节点分布
          </el-button>
          <el-button type="text" icon="el-icon-caret-right" @click="handleExe(row)" v-hasPerms="['sched:job:execute']">执行</el-button>
          <el-button type="text" icon="el-icon-tickets" @click="handleLog(row)" v-hasPerms="['sched:jobLog:list']">日志</el-button>
          <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)" v-hasPerms="['sched:job:edit']">修改</el-button>
          <el-dropdown v-hasPerms="'sched:job:remove'">
            <el-link type="primary" icon="el-icon-arrow-down" :underline="false" class="el-button--small">更多</el-link>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-hasPerms="['sched:job:remove']">
                <el-button type="text" icon="el-icon-delete" @click="handleDelete(row)">删除</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPerms="['sys:user:resetPwd']">
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="!loading" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"  @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <EditScheduleJob ref="formDialog" @ok="getList" />
    <!-- 导出对话框 -->
    <ToExport ref="exportDialog" />

    <JobNodeDetail ref="jobNodeDetail" />

  </div>
</template>

<script>
import {httpGet, httpDel, httpPost} from '@/api';
import { IndexListMixin } from '@/components/mixins/IndexListMixin';
import ToExport from '@/components/download/ToExport';
import EditScheduleJob from './edit';
import JobNodeDetail from './nodeDetail';
import JobScheduleStatus from './JobScheduleStatus';

export default {
  name: "ScheduleJob",
  mixins:[IndexListMixin],
  components: {
	  ToExport,
    EditScheduleJob,
    JobNodeDetail,
    JobScheduleStatus
  },
  data() {
    return {
      exeBtnLoading: false,
      // 查询参数
      queryParams: {
        jobNameKey: null,
        jobGroup: null,
        status: null,
        pageNum: 1,
        pageSize: 10
      },
      url: {
      	exportXls: '/sched/job/export',
        base: '/sched/job/',
      	list: '/sched/job/list',
      	editStatus: '/sched/job/editStatus',
        execute: '/sched/job/execute/'
      }
    };
  },
  created() {
    this.getList();
  },
  mounted(){
  },
  methods: {
    getList() {
      this.loadData(this.url.list);
    },

    // 任务状态修改
    handleStatusChange(row) {
      let text = row.status == "1" ? "启用" : "停用";
      this.$confirm('确认要' + text + '这个任务吗?', "确认", {type: "warning"}).then(()=>{
        return httpPost(this.url.editStatus, {id:row.id, status: row.status});
      }).then(() => {
        this.msgSuccess(text + "成功");
        this.getList();
      }).catch(()=>{
        row.status = row.status == 0 ? 1 : 0;
      });
    },

    showNodeDetail(row){
      this.$refs.jobNodeDetail.show(this.$stringFormat('【{0}】节点分布', row.jobName), row.id);
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.formDialog.toAdd("添加调度任务");
    },

    /** 复制按钮操作 */
    handleCopy() {
      let data = Object.assign({}, this.selectedRows[0]);
      data.id = null;
      this.$refs.formDialog.toAdd("复制-新增调度任务", data);
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.selectedRowKeys
      this.$refs.formDialog.toUpdate("修改调度任务", httpGet(this.url.base + id));
    },

    /** 删除按钮操作 */
    handleDelete(row) {
		const id = row.id || this.selectedRowKeys
		this.deleteData(this.url.base, id);
    },

    /** 导出按钮操作 */
    handleExport() {
      this.exportData('定时任务导出', this.url.exportXls, this.queryParams);
    },

    handleExe(row){
      this.$confirm("确认要立即执行任务?", "确认", {type: "warning"}).then(() => {
        this.exeBtnLoading = true;
        const id = row.id || this.selectedRowKeys
        httpPost(this.url.execute + id, {}).then(res=>{
          this.getList();
          this.msgSuccess("触发成功，请稍后在日志查看结果！");
        }).finally(()=>{
          this.exeBtnLoading = false;
        });
      })
    },

    /** 任务日志列表查询 */
    handleLog(row) {
      const id = row.id || this.selectedRowKeys[0]
      if(id){
        this.$router.push("/schedule/jobLog0/"+id);
      }else{
        this.$router.push("/schedule/jobLog");
      }
    }

  }
};
</script>
